package com.reyco1.gadget
{
	/*
	imports
	*/
	import com.reyco1.extension.DispatchExtension;
	import com.reyco1.util.IntervalUtil;
	import com.reyco1.events.IntervalEvent;
	import flash.display.MovieClip;
	import com.reyco1.util.DisplayObjectUtil;
	import flash.geom.Rectangle;
	import com.reyco1.events.SliderEvent;
	import com.reyco1.gadget.EnhancedButton;
	import com.reyco1.events.ButtonEvent;
	/*
	class
	*/
	public class Slider extends DispatchExtension
	{
		/*
		variables
		*/
		private var objDragInterval:IntervalUtil;
		private var mcSlider:MovieClip;
		private var mcTrack:MovieClip;
		private var mcBar:MovieClip;
		private var sliderBar:EnhancedButton;
		private var numValue:Number;
		private var numMin:Number;
		private var numMax:Number;
		
		/*
		constructor
		*/
		public function Slider():void
		{
		}
		
		/*
		functions
		*/
		
		/**
		 * Sets up the slider with the provided MovieClips
		 * @param $parameters
		 * 
		 */		
		public function setup($parameters:Object):void
		{
			this.mcTrack = $parameters.track;
			this.mcBar = $parameters.bar;
			this.sliderBar = new EnhancedButton(this.mcBar);
			this.mcSlider = $parameters.slider;			
			this.numMin = $parameters.min || 0;
			this.numMax = $parameters.max || 100;
			this.objDragInterval = new IntervalUtil("Slider", this.calculateValue);
			this.objDragInterval.outputSilenced = true;
			this.initSlider();
		}
		
		private function initSlider():void
		{
			this.mcBar.x = this.mcTrack.x;
			this.mcBar.y = this.mcTrack.y;
			var dragRestrictions:Rectangle = new Rectangle(this.mcTrack.x, this.mcTrack.y, this.mcTrack.width, 0)
			
			DisplayObjectUtil.makeDragable(this.mcBar, dragRestrictions);
			
			this.sliderBar.addButtonListener(ButtonEvent.DOWN, this.startDragging);
			this.sliderBar.addButtonListener(ButtonEvent.UP, this.stopDragging);
			this.sliderBar.addButtonListener(ButtonEvent.RELEASE_OUTSIDE, this.stopDragging);
		}
		
		private function startDragging($event:ButtonEvent):void
		{
			this.objDragInterval.start({delay:0.01});
			this.dispatchEvent(new SliderEvent(SliderEvent.START_DRAG, {}));
		}
		
		private function stopDragging($event:ButtonEvent):void
		{
			this.objDragInterval.stop();
			this.dispatchEvent(new SliderEvent(SliderEvent.STOP_DRAG, {}));
		}
		
		private function calculateValue():void
		{
			this.numValue = this.mcBar.x / this.mcTrack.width * (this.numMax - this.numMin) + this.numMin;
			this.dispatchEvent(new SliderEvent(SliderEvent.UPDATE, {value:this.numValue, slider:this.mcSlider}));
		}
		
		/**
		 * Returns the current value of the slider
		 * @return Number
		 * 
		 */		
		public function get value():Number
		{
			return Math.round(this.numValue);
		}
		
		/**
		 * Sets the value for the slider 
		 * @param $num : Number
		 * 
		 */		
		public function set value($num:Number):void
		{
			this.numValue = $num;
			this.numValue = Math.max(this.numValue, this.numMin);
			this.numValue = Math.min(this.numValue, this.numMax);
			this.mcBar.x = this.mcTrack.width * ($num - this.numMin) / (this.numMax - this.numMin);
		}
	}
}